using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Threading;
using System.ComponentModel;

// Logging
//#if LOG4NET
using log4net;
using log4net.Config;
using log4net.Core;
//#endif

namespace Icodeon.Scorm2CC.Console
{

    public class MainConsole {

        private static ILog log;
        private static Scorm2ccConverter converter;

        public static void Program(
            DirectoryInfo input,
            DirectoryInfo output,
            FileInfo xslFileForScorm1p2,
            FileInfo xslFileForScorm2004)
        {
            // Logging
            Type type = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
            log = LogManager.GetLogger(type);

            // Version
            Assembly assembly = Assembly.GetExecutingAssembly();
            Version vrs = assembly.GetName().Version;
            string longVersionFromVrs = String.Format("{0}.{1}.{2}.{3}", vrs.Major, vrs.Minor, vrs.Build, vrs.MinorRevision);

            System.Console.WriteLine("SCORM 2 CC Version " + longVersionFromVrs);

            FileInfo[] inputFiles = input.GetFiles("*.zip");

            // Create a background thread
            BackgroundWorker backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += new DoWorkEventHandler(DoWork);

            converter = new Scorm2ccConverter();

            converter.InputFiles = inputFiles;
            converter.Output = output;
            converter.XslFileForScorm1p2 = xslFileForScorm1p2;
            converter.XslFileForScorm2004 = xslFileForScorm2004;

            converter.ConversionStart += new ConversionStartEventHandler(Monitor_ConversionStart);
            converter.ConversionComplete += new ConversionCompleteEventHandler(Monitor_ConversionComplete);
            converter.ConversionError += new ConversionErrorEventHandler(Monitor_ConversionError);
            converter.ConversionLog += new ConversionLogEventHandler(Monitor_ConversionLog);

            // Kick off the Async thread
            backgroundWorker.RunWorkerAsync();
        }

        private static void DoWork(object sender, DoWorkEventArgs e)
        {
            converter.StartConversion();
        }

        private static void Monitor_ConversionStart(object sender, ConversionStartEventArgs e)
        {
            System.Console.WriteLine("SCORM 2 CC conversion running...");
        }

        private static void Monitor_ConversionComplete(object sender, ConversionCompleteEventArgs e)
        {
            System.Console.WriteLine("SCORM 2 CC conversion completed.");
        }

        private static void Monitor_ConversionError(object sender, ConversionErrorEventArgs e)
        {
            System.Console.WriteLine("SCORM 2 CC conversion error: " + e.Exception.Message);
            log.Error(e.Exception.Message, e.Exception);
        }

        private static void Monitor_ConversionLog(object sender, ConversionLogEventArgs e)
        {
            System.Console.WriteLine("SCORM 2 CC log: " + e.Message);

            if (e.LogLevel.Equals(Level.Debug) &&  log.IsDebugEnabled)
            {
                log.Debug(e.Message);
            }
            else if (e.LogLevel.Equals(Level.Info) && log.IsInfoEnabled)
            {
                log.Info(e.Message);
            }
            else if (e.LogLevel.Equals(Level.Warn) && log.IsWarnEnabled)
            {
                log.Warn(e.Message);
            }
            else if (e.LogLevel.Equals(Level.Error) && log.IsErrorEnabled)
            {
                log.Error(e.Message, e.Exception);
            }
            else if (e.LogLevel.Equals(Level.Fatal) &&  log.IsFatalEnabled)
            {
                log.Fatal(e.Message, e.Exception);
            }
        }
    }
}
